<?php 
$page_title = "21 Game - Login";
include("header.php");
if(isset($_SESSION['username'])){
	header("Location: index.php");
} ?>

<!-- page header -->
<div class="page-header">
	<div class="container">
		<h1><span style="color: red;">Login</span></h1>
	</div>
</div>
<!-- end page header -->

<?php 

if($_SERVER['REQUEST_METHOD'] == "POST"){

	require("sql/connection.php");
	require("utilities/functions.php");

	$errors = array();	//Initialize errors array

	if(empty($_POST['email']) OR empty($_POST['password'])){
		echo "<p class='text-danger'>Please make sure you filled all the fields.</p>";
		echo "<a href='login.php'>Back</a>";
	}
	else{
		if(loginUser($dbc, $_POST['email'], $_POST['password'])){
			echo "<p class='lead'>You have signed in into your account.";
			header( "Refresh:2; url=index.php" );
		}
		else{
			echo "<p class='lead'>Incorrect login, please try again.";
			header( "Refresh:1; url=login.php" );
		}
	}
echo "</div>";
}
else{
	?>
<!--content-->
<div class="container">
	<div class="col-lg-3">
		<form action="" method="post" role="form">
			<div class="form-group">
				<label for="username">Email</label>
				<input type="text" class="form-control" name="email" placeholder="Insert your email">
			</div>
			<div class="form-group">
				<label for="password">Password</label>
				<input type="password" class="form-control" name="password" placeholder="Insert your password">
			</div>
			<input type="submit" class="btn btn-primary" style="background:red" value="Sign in">
		</form>
	</div>
</div>
<!--end content-->
	<?php
}
 ?>
<?php include("footer.php"); ?>